package com.suma.platform.core.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.nfsq.platform.utils.bean.ResultBean;
import com.suma.platform.core.service.RedisService;

/**
 * @author suma
 * @date 2018/1/16 下午2:53
 */
@RestController
@RequestMapping(value = "/redis")
public class RedisTestController {

	@Autowired
	private RedisService redisService;

	@RequestMapping(value = "/test", method = { RequestMethod.GET, RequestMethod.POST })
	public ResultBean redisTest() {
		StringBuffer sb = new StringBuffer();
		redisService.set("str", "str");
		sb.append("str=").append(redisService.get("str").toString()).append(",");
		redisService.hmSet("hmset", "key", "val");
		sb.append("hmset=").append(redisService.hmGet("hmset", "key")).append(",");
		redisService.lPush("list", "val");
		sb.append("list=").append(redisService.lRange("list", 0, 1).toString()).append(",");
		redisService.add("set", "val");
		sb.append("set=").append(redisService.setMembers("set").toString()).append(",");
		redisService.zAdd("zset", "val1", 1);
		redisService.zAdd("zset", "val2", 2);
		sb.append("zset=").append(redisService.rangeByScore("zset", 1, 2)).append(",");
		return new ResultBean<>(sb.toString());
	}

	public static void main(String[] args) {
		String s1 = "Programming";
		String s2 = new String("Programming");
		String s3 = "Program";
		String s4 = "ming";
		String s5 = "Program" + "ming";
		String s6 = s3 + s4;
		String s7 = new StringBuilder("Programming").toString();
		String s8 = new StringBuilder("Program").append("ming").toString();


		System.out.println(s1 == s2); //false  s1被赋予字符串Programming后发现常量池中并没有这一字符串，就将其
		//放入常量池
		System.out.println(s1 == s5);//true   //字符串的+操作其本质是创建了StringBuilder对象进行append操作，
		//然后将拼接后的StringBuilder对象用toString方法处理成String对象，s5拼接完成后发现常量池中已经有字符串Programming
		//所以s5直接取常量池中的字符串所以返回true
		System.out.println(s1 == s6);//false    这个暂时没搞懂
		System.out.println(s1 == s5.intern());//true
		System.out.println(s1 == s6.intern());//true
		System.out.println(s2 == s2.intern());//false  因为s2  new了一个字符串对象，在堆中单独开辟一块区域，并不是常量池
		//中对象 所以返回false
		System.out.println(s7.intern()==s7);//false //返回false因为new出来的对象单独开辟一块区域放在java堆中，与常量池中的不同
		System.out.println(s8.intern()==s8);///false   这个单独执行时是true  放在这里一起执行是false，个人推测
		//是因为常量池中已经存在字符串Programming，而s8是new出来的，他拼接完成之后，其地址与常量池中字符串的地址
		//不同，返回false


	}
}
